/*
 * Copyright (c) 2007-2008, Thomas Krause (tho.krause@gmail.com)
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of gidoo nor the names of its contributors may be used to
 *       endorse or promote products derived from this software without specific
 *       prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.gidoo.cms.helper;

import java.util.LinkedList;
import java.util.List;
import org.gidoo.cms.GidooCMS;
import org.gidoo.cms.data.Folder;
import org.gidoo.cms.data.Page;
import org.gidoo.cms.dbconnection.DatabaseConnectionException;

/**
 * Somer helper Tools which depend on classes from gidooCMS.
 * @author <a href="mailto:tho.krause@gmail.com">Thomas Krause</a>
 */
public class Tools 
{
  
  /**
   * Will do some cleanup on the path <br>
   * More concrete it will remove leading and trailing slashes 
   * e.g. "abc/def/ghi" is clean but "/abc/" is not)
   * @param path The original path
   * @return The "new" path
   */
  public static String cleanupFolderPath(String path)
  {
    String newPath = path;
    if(newPath.startsWith("/"))
    {
      newPath = newPath.substring(1);
    }
    
    if(newPath.endsWith("/"))
    {
      newPath = newPath.substring(0, path.length() -1);
    }
    
    return newPath;
  }
  
  /**
   * This function will traverse through the folder and it's parent (including
   * the section) an is building the path from this.
   * @param folder The folder you want to get the URL for.
   * @return The URL (without server and context of course).
   */
  public static String getURLOfFolder(Folder folder)
  {
    String result = "";
    Folder curFolder = folder;
    
    while(curFolder != null)
    {
      String n = curFolder.getName();
      if(!"".equals(n))
      {
        result = curFolder.getName() + "/"+ result;
      }
      curFolder = curFolder.getParent();
    }
    
    result = folder.getSection().getUrl() + "/" + result;
    
    return result;
  }
  
  /**
   * Returns an array of all folder are successors of the folder.
   * @param folder The folder.
   * @param the application instance
   * @return An array of all successors.
   */
  public static Folder[] getAllSubfolders(Folder folder, GidooCMS app) throws DatabaseConnectionException
  {
    LinkedList<Folder> list = new LinkedList<Folder>();
    getSubfoldersRecursive(folder, list, app);
    
    Folder[] result = new Folder[]{};
    result = list.toArray(result);
    return result;
  }
  
  private static void getSubfoldersRecursive(Folder parent, List<Folder> list, GidooCMS app) throws DatabaseConnectionException
  {
    if(parent != null && list != null && app != null)
    {
      List<Folder> children = app.getDb().getChildrenOfFolder(parent);
      for(Folder f : children)
      {
        list.add(f);
        getSubfoldersRecursive(f, list, app);
      }
    }
  }
  
  /** Returns true if the folder has a published page and it is not empty */
  public static boolean isPublished(Folder folder)
  {
    if(folder != null)
    {
      Page p = folder.getPublishedPage();
      if(p != null)
      {
        String xml = p.getContentAsXML();
        if(xml != null && !"".equals(xml))
        {
          return true;
        }
      }
    }
    
    return false;
  }
  
}
